package com.qiqijuice.sales;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;

public class ProductListView extends Composite {
	public static int EVT_PRODUCT_SELETED = 0x4000;
	
	private static int MAX_CHOICES = 200;
	private static int BUTTON_WIDTH = 150;
	private static int BUTTON_HEIGHT = 150;
	private static int HORIZONTAL_PADDING = 10;
	private static int VERTICAL_PADDING = 10;
	private static int X_GAP = 5;
	private static int Y_GAP = 5;
	
	private Button[] choices = null; 
	private ProductCategory curCategory;
	private int curPage;
	private int columnsPerPage;
	private int rowsPerPage;
	
	
	private boolean inResize = false;
	
	public ProductListView(Composite parent) {
		super(parent, SWT.NONE);
		
		setLayout(new MigLayout("",""));

		addControlListener(new ControlAdapter() {

			@Override
			public void controlResized(ControlEvent e) {
				contentResized();
			}
			
		});
		
		/*
		for (int i=0; i<MAX_CHOICES; ++i) {
			Button b = new Button(this, SWT.PUSH);
			b.setVisible(false);
			choices[i] = b;
		}
		*/
		curPage = 0;
	}

	public void viewProductCategory(ProductCategory category) {
		if (curCategory != category) {
			curCategory = category;
			curPage = 0;
			updateChoices();
		}
	}
	
	@SuppressWarnings("serial")
	private void updateChoices() {
		if (curCategory != null) {
			MigLayout layout = (MigLayout) getLayout();
			layout.setLayoutConstraints(String.format("ins 0 0 0 0, wrap %d", columnsPerPage));
			
			ObjectContainer oc = Db4oContainerFactory.getDB();
			ObjectSet<Product> result = oc.query(new Predicate<Product>() {

				@Override
				public boolean match(Product arg0) {
					return arg0.getCategory() == curCategory;
				}
				
			});
			
			int count = columnsPerPage * rowsPerPage;
			if (count > MAX_CHOICES)
				count = MAX_CHOICES;
			
			int start_idx = count * curPage;
			int cur_idx = 0;
			
			while(result.hasNext() && cur_idx < start_idx + count) {
				Product p = result.next();
				
				if (cur_idx >= start_idx) {
					choices[cur_idx-start_idx].setText(p.getName());
					choices[cur_idx-start_idx].setVisible(true);
					//choice[cur_idx-start_idx].setSize(BUTTON_DIM, 50);
					choices[cur_idx-start_idx].setLayoutData(
							String.format("width %1$d:%1$d:%1$d, height %2$d:%2$d:%2$d", 
									BUTTON_WIDTH, BUTTON_HEIGHT));
					
					choices[cur_idx-start_idx].setData(p);
				}
				
				++cur_idx;
			}
			
			for (; cur_idx < start_idx + count; ++cur_idx) {
				choices[cur_idx-start_idx].setText("");
				choices[cur_idx-start_idx].setVisible(false);
				choices[cur_idx-start_idx].setData(null);
			}
			
		}
		else {
			for (Button b : choices) {
				if (b != null) {
					b.setVisible(false);
					b.setText("");
				}
			}
		}
		
		layout();
	}

	protected void contentResized() {
		
		if (inResize)
			return;
		inResize = true;
		
		int width = getSize().x;
		int height = getSize().y;
		
		columnsPerPage = (width - 2 * HORIZONTAL_PADDING - X_GAP) / (BUTTON_WIDTH + X_GAP);
		rowsPerPage = (height - 2 * VERTICAL_PADDING - Y_GAP) / (BUTTON_HEIGHT + Y_GAP);
		
		curPage = 0;
		
		if (choices == null) {
			choices = new Button[MAX_CHOICES];
			for (int i=0; i<MAX_CHOICES; ++i) {
				Button b = new Button(this, SWT.PUSH);
				b.addSelectionListener(new SelectionAdapter() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						Event ee = new Event();
						ee.data = e.widget.getData();
						notifyListeners(EVT_PRODUCT_SELETED, ee);
					}
					
				});
				b.setLayoutData("hidemode 3");
				b.setVisible(false);
				choices[i] = b;
			}
		}
		
		updateChoices();
		inResize = false;
	}

}
